package mop.gui.reservation;

import java.util.LinkedList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.table.TableCellRenderer;
import mop.controller.MakeResourceReservationController;
import mop.gui.GUI;
import mop.gui.component.JTableButtonModel;
import mop.gui.component.JTableButtonMouseListener;
import mop.gui.component.JTableButtonRenderer;
import mop.model.task.TaskInterface;

/**
 * List of reservations.
 *
 * @author  MOP Team 7
 */
public class ReservationPanel extends javax.swing.JPanel
{
    /**
     * Default constructor.
     */
    public ReservationPanel()
    {
        initComponents();
    }

    /**
     * Constructor.
     *
     * @param   gui
     * @param   makeResourceReservationController
     */
    public ReservationPanel(GUI gui, MakeResourceReservationController makeResourceReservationController)
    {
        initComponents();
        this.setGui(gui);
        this.makeResourceReservationController = makeResourceReservationController;
        tableUnfinishedTasks.addMouseListener(new JTableButtonMouseListener(tableUnfinishedTasks));
    }

    /***************************************************************************
     *                          GUI reference
     ***************************************************************************/

    private GUI gui;

    /**
     * Sets the GUI.
     *
     * @param   gui
     * @post    gui is set.
     */
    private void setGui(GUI gui)
    {
        this.gui = gui;
    }

    /**
     * Returns the GUI reference.
     *
     * @return  this.gui
     */
    public GUI getGui()
    {
        return this.gui;
    }

    /***************************************************************************
     *                             reservations
     ***************************************************************************/

    private MakeResourceReservationController makeResourceReservationController;

    /**
     * Loads projects in projects table.
     */
    public void update()
    {
        LinkedList<TaskInterface> tasks = new LinkedList<TaskInterface>();
        tasks = (LinkedList<TaskInterface>) makeResourceReservationController.getUnfinishedTasks();

        JTableButtonModel tableModel = new JTableButtonModel();

        String[] columns = {"Task Id", "Make Resource Reservation"};
        Object[][] rows = generateTaskRows(tasks);

        tableModel.setColumns(columns);
        tableModel.setRows(rows);

        tableUnfinishedTasks.setModel(tableModel);
        TableCellRenderer defaultRenderer = tableUnfinishedTasks.getDefaultRenderer(JButton.class);
        JTableButtonRenderer buttonRenderer = new JTableButtonRenderer(defaultRenderer);
        tableUnfinishedTasks.setDefaultRenderer(JButton.class, buttonRenderer);
    }

    /**
     * Generates the rows of the unfinished tasks table.
     *
     * @param   tasks
     *          List of task interfaces.
     * @return  The rows for the table.
     */
    private Object[][] generateTaskRows(List<TaskInterface> tasks)
    {
        Object[][] rows = new Object[tasks.size()][2];

        for ( int i = 0 ; i < tasks.size() ; i++ ) {

            rows[i][0] = tasks.get(i).getId();

            rows[i][1] = new JButton("Make Resource Reservation");
            ((JButton)rows[i][1]).addMouseListener(new MakeReservationAction(gui, makeResourceReservationController, tasks.get(i)));

        }

        return rows;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblReservation = new javax.swing.JLabel();
        projectsPane = new javax.swing.JScrollPane();
        tableUnfinishedTasks = new javax.swing.JTable();

        setPreferredSize(new java.awt.Dimension(517, 569));

        lblReservation.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        lblReservation.setText("Make resource reservations for tasks here"); // NOI18N

        tableUnfinishedTasks.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        projectsPane.setViewportView(tableUnfinishedTasks);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(projectsPane, javax.swing.GroupLayout.DEFAULT_SIZE, 497, Short.MAX_VALUE)
                    .addComponent(lblReservation))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblReservation)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(projectsPane, javax.swing.GroupLayout.DEFAULT_SIZE, 519, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel lblReservation;
    private javax.swing.JScrollPane projectsPane;
    private javax.swing.JTable tableUnfinishedTasks;
    // End of variables declaration//GEN-END:variables

}
